package com.bss.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name = "building")
public class Building extends com.bss.AbstractNamedEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2050960846273084455L;

	private int id;

	@NotNull
	@Size(min = 1, max = 45)
	private String name;

	@NotNull
	@Min(value = 1)
	private Integer floors;

	private List<Room> rooms = new ArrayList<Room>(0);

	public Building() {
	}

	public Building(String name, int floors, List<Room> newRooms) {
		this.name = name;
		this.floors = floors;
		this.rooms.addAll(newRooms);
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return this.id;
	}

	@Override
	public void setId(final int id) {
		this.id = id;
	}

	@Column(name = "name", length = 45, unique = true, nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	@Column(name = "floors", nullable = false)
	public Integer getFloors() {
		return this.floors;
	}

	public void setFloors(final Integer floors) {
		this.floors = floors;
	}

	@OrderBy("roomNumber ASC")
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "building", cascade = CascadeType.PERSIST)
	public List<Room> getRooms() {
		return this.rooms;
	}

	public void setRooms(final List<Room> rooms) {
		this.rooms = rooms;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date", length = 19)
	public Date getDate() {
		return this.date;
	}

}
